<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: google_analytics.js.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

if(isset($_GET['page']) && $_GET['page'] == 'checkout_success.php') {
	$content = 'var _gaq = _gaq || [];_gaq.push([\'_setAccount\', \''.GOOGLE_ANAL_CODE.'\']);_gaq.push([\'_gat._anonymizeIp\']);_gaq.push([\'_trackPageview\']);';

	require_once(DIR_WS_CLASSES.'class.order.php');
	require_once(DIR_FS_INC.'inc.get_attributes_model.php');
	require_once(DIR_FS_INC.'inc.get_product_path.php');

	$orders = $db->db_query("SELECT orders_id FROM ".TABLE_ORDERS." WHERE customers_id = '".$_SESSION['customer_id']."' ORDER BY orders_id DESC LIMIT 1");

	$order = new order($orders->fields['orders_id']);

	$order = $db->db_query("SELECT
		        				products_id,
		        				orders_products_id,
		        				products_model,
		        				products_name,
		        				products_price,
		        				final_price,
		        				products_quantity
		        			FROM
		        				".TABLE_ORDERS_PRODUCTS."
		        			WHERE
		        				orders_id = '".$orders->fields['orders_id']."'");

	while(!$order->EOF) {
		$attributes = $db->db_query("SELECT
				        				products_options,
				        				products_options_values,
				        				price_prefix,
				        				options_values_price
				        			FROM
				        				".TABLE_ORDERS_PRODUCTS_ATTRIBUTES."
				        			WHERE
				        				orders_products_id = '".$order->fields['orders_products_id']."'");
		$attributes_data = '';
		$attributes_model = '';
		$i = 0;
		while(!$attributes->EOF) { 
			$i++;
			$attributes_data .= ($i > 1 ? ' | ' : '').$attributes->fields['products_options'].' '.$attributes->fields['products_options_values'];
			$attributes->MoveNext();
		}

		$CatName = $db->db_query("SELECT
										categories_name
									FROM
										".TABLE_CATEGORIES_DESCRIPTION."
									WHERE
										categories_id = '".get_product_path($order->fields['products_id'])."'
									AND
										language_id = '".$_SESSION['languages_id']."'");

		$content .= '_gaq.push([\'_addItem\',\''.$orders['orders_id'].'\',\''.$order->fields['products_id'].'\',\''.$order->fields['products_name'].'\',\''.(!empty($attributes_data) ? $attributes_data : $CatName->fields['categories_name']).'\',\''.number_format($order->fields['products_price'], 2, '.', '').'\',\''.$order->fields['products_quantity'].'\']);';
		$order->MoveNext();
	}

	$ot_total = $db->db_query("SELECT
									value
								FROM
									".TABLE_ORDERS_TOTAL."
								WHERE
									orders_id='".$orders->fields['orders_id']."'
								AND
									class = 'ot_total' ");

	$ot_shipping = $db->db_query("SELECT
										value
									FROM
										".TABLE_ORDERS_TOTAL."
									WHERE
										orders_id='".$orders->fields['orders_id']."'
									AND
										class = 'ot_shipping' ");

	$ot_tax = $db->db_query("SELECT
									value
								FROM
									".TABLE_ORDERS_TOTAL."
								WHERE
									orders_id='".$orders->fields['orders_id']."'
								AND
									class = 'ot_tax' ");
	$content .= '_gaq.push([\'_addTrans\',\''.$orders['orders_id'].'\',\''.STORE_NAME.'\',\''.number_format($ot_total->fields['value'], 2, '.', '').'\',\''.number_format($ot_tax->fields['value'], 2, '.', '').'\',\''.number_format($ot_shipping->fields['value'], 2, '.', '').'\',\''.$order->delivery['city'].'\',\''.$order->delivery['state'].'\',\''.$order->delivery['country'].'\']);_gaq.push([\'_trackTrans\']);';

	$content .=	'(function() {
			var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
			ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
			var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
			})();';
} else {
	$content = 'var _gaq=_gaq||[];_gaq.push([\'_setAccount\',\''.GOOGLE_ANAL_CODE.'\']);_gaq.push([\'_gat._anonymizeIp\']);_gaq.push([\'_trackPageview\']);(function(){var ga=document.createElement("script");ga.type="text/javascript";ga.async=true;ga.src=("https:"==document.location.protocol?"https://ssl":"http://www")+".google-analytics.com/ga.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(ga,s);})();';
}